package com.geostar.constructionland.attachment.service.impl;
import com.geostar.constructionland.attachment.entity.RepEntity;
import com.geostar.constructionland.attachment.service.IStorageService;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSDownloadStream;
import com.mongodb.client.gridfs.model.GridFSFile;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSInputFile;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsResource;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.UUID;

/**
 * @author : yell
 * @Description: MONGODB存储实现
 * @date : 2020/01/06 11:11
 */
@Slf4j
@Service(value = "mongoDBStorageService")
public class MongoDBStorageServiceImpl implements IStorageService {
    private GridFsTemplate gridFsTemplate;
    private GridFSBucket gridFSBucket;
    private GridFS gridFS;
    @Autowired
    public void setGridFsTemplate(GridFsTemplate gridFsTemplate) {
        this.gridFsTemplate = gridFsTemplate;
    }
    @Autowired
    public void setGridFSBucket(GridFSBucket gridFSBucket) {
        this.gridFSBucket = gridFSBucket;
    }
    @Autowired
    public void setGridFS(GridFS gridFS) {
        this.gridFS = gridFS;
    }

    @Override
    public RepEntity uploadAttach(byte[] attachBytes,long attachSize,String fileName,String fileExt) {
        String fileId = UUID.randomUUID().toString().replace("-","");
        GridFSInputFile gridFSInputFile = gridFS.createFile(attachBytes);
        gridFSInputFile.setFilename(fileName);
        gridFSInputFile.put("fileId",fileId);
        gridFSInputFile.save();
        return new RepEntity("200",fileId);
    }

    @Override
    public byte[] getByteArrayByFilePath(String filePath) {
        GridFSFile gridFSFile = getGridFsFile(filePath);
        GridFsResource gridFsResource = getGridFsResource(gridFSFile);
        if(gridFsResource != null){
            try {
                return IOUtils.toByteArray(gridFsResource.getInputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public boolean deleteAttachById(String filePath) {
        gridFsTemplate.delete(Query.query(Criteria.where("fileId").is(filePath)));
        return true;
    }


    public GridFSFile getGridFsFile(String attachId){
        GridFSFile file = gridFsTemplate.findOne(Query.query(Criteria.where("fileId").is(attachId)));
        return file;
    }

    public GridFsResource getGridFsResource(GridFSFile file){
        if(file != null) {
            GridFSDownloadStream gridFSDownloadStream = gridFSBucket.openDownloadStream(file.getObjectId());
            GridFsResource gridFsResource = new GridFsResource(file, gridFSDownloadStream);
            return gridFsResource;
        }
        return null;
    }
}
